package com.platform.mall.controller.admin;

import com.platform.mall.common.exception.BadRequestException;
import com.platform.mall.common.result.Result;
import com.platform.mall.common.valid.Create;
import com.platform.mall.common.valid.Update;
import com.platform.mall.model.entity.SysDict;
import com.platform.mall.model.query.DictQueryParam;
import com.platform.mall.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

@Api(tags = "字典接口")
@RestController
@RequestMapping("/api/v1/dict")
@RequiredArgsConstructor
public class SysDictController {

    private final ISysDictService dictService;
    private static final String ENTITY_NAME = "dict";

    @ApiOperation("查询字典")
    @GetMapping("/page")
    public Result findForPage(DictQueryParam query, Pageable pageable) {
        return Result.success(dictService.findForPage(query, pageable));
    }

    @ApiOperation("查询字典")
    @GetMapping("/list")
    public Result list(DictQueryParam query) {
        return Result.success(dictService.findForList(query));
    }

    @ApiOperation("新增字典")
    @PostMapping("/add")
    public Result create(@Validated({Create.class}) @RequestBody SysDict resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        dictService.save(resources);
        return Result.success();
    }

    @ApiOperation("修改字典")
    @PutMapping("/update")
    public Result update(@Validated({Update.class})@RequestBody SysDict resources) {
        dictService.updateById(resources);
        return Result.success();
    }

    @ApiOperation("删除字典")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Set<Long> ids) {
        dictService.removeByIds(ids);
        return Result.success();
    }

}
